//
//  WLSVisitorView.swift
//  1-微博
//
//  Created by 王隆善 on 16/7/28.
//  Copyright © 2016年 王隆善. All rights reserved.
//

import UIKit
//导入SnapKit
import SnapKit

class WLSVisitorView: UIView {

    //自定义视图3部曲
    //1,重写父类的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    //2,定义一个添加子类和设置子类控件约束的方法
    private func setupUI()
    {
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        //添加到视图
        addSubview(turnImageView)
        addSubview(maskImageView)
        addSubview(homeIconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //设置约束
        //蒙板约束
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
        //小房子约束
        homeIconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
        //旋转图片的约束
        turnImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
        //提示语约束
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(turnImageView.snp_bottom).offset(10)
            make.width.equalTo(216)
        }
        //注册按钮约束
        registerButton.snp_makeConstraints { (make) in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        //登录按钮约束
        loginButton.snp_makeConstraints { (make) in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
      
    }
    
    
    //MARK:--首页的动画效果
    func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //toValue
        animation.toValue = 2*M_PI
        
        //次数和事件
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        
        //当切换视图时,动画被完全移除
        animation.removedOnCompletion = false
        
        //添加到layer里
        turnImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    //MARK:--对外提供一个方法,来改变视图
    func setupInfo (imageName: String,message: String,isHome: Bool = false)
    {
        homeIconImageView.image = UIImage(named: imageName)
        
        messageLabel.text = message
        
        if isHome
        {
            turnImageView.hidden = false
            startAnimation()
        }
        else{
            turnImageView.hidden = true
        }
    }
    
    //MARK:--登录
    func login(){
        print("登录")
    }
    
    //MARK:--注册
    func register()
    {
        print("注册")
    }
    
    //蒙板
    private lazy var maskImageView:UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
    }()
    
    
    //首页的小房子
    private lazy var homeIconImageView: UIImageView = {
        
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        return img
    }()
    
    //转动的图片
    private lazy var turnImageView: UIImageView = {
       
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
    }()
    
    //提示文字
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel()
        
        //设置属性
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.text = "关注一些人"
        label.textAlignment = .Center
        
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //设置背景
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加点击事件
        button.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //设置背景
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加点击事件
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
